from blls.annotation_blls.annotation_bll import AnnotationBLL
from handlers.base_handler import BaseHandler
from utils.check_params_utlil import CheckParams
from utils.handler_util import HandlerUtil


class GetAnnotationHandler(BaseHandler):
    def post(self):
        check = CheckParams(params=self.request_data,
                            conditions=[["data_id", True, ""]
                                        ]).check()
        if not check.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check.message))
        res = AnnotationBLL(request=self).get_by_data_id(check.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))
class SaveAllAnnotationsHandler(BaseHandler):
    def post(self):
        check = CheckParams(params=self.request_data,
                            conditions=[["annotations",False,""],
                                        ["data_id", True, ""]
                                        ]).check()
        if not check.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check.message))
        res = AnnotationBLL(request=self).save_all(check.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))

class DeleteAnnotationsHandler(BaseHandler):
    def post(self):
        check = CheckParams(params=self.request_data,
                            conditions=[["annotation_id", True, ""]
                                        ]).check()
        if not check.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check.message))
        res = AnnotationBLL(request=self).delete_by_id(check.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))

class ClassifyAnnotationHandler(BaseHandler):
    def post(self):
        check = CheckParams(params=self.request_data,
                            conditions=[["data_id", True, ""],
                                        ["label_id", True, ""],
                                        ["result",False, ""],
                                        ["frontend_id",False,""],
                                        ["type",False,""]
                                        ]).check()
        if not check.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check.message))
        res = AnnotationBLL(request=self).classify_annotation(check.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))

class UpdateAnnotationHandler(BaseHandler):
    def post(self):
        check = CheckParams(params=self.request_data,
                            conditions=[["annotation_id", True, ""],
                                        ["frontend_id", True, ""],
                                        ["project_id", True, ""],
                                        ["result", True, ""],
                                        ["data_id", True, ""],
                                        ["type", True, ""]
                                        ]).check()
        if not check.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check.message))
        res = AnnotationBLL(request=self).update_annotation(check.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))

class AnnotationDetailHandler(BaseHandler):
    def post(self):
        check = CheckParams(params=self.request_data,
                            conditions=[["annotation_id", True, ""]
                                        ]).check()
        if not check.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check.message))
        res = AnnotationBLL(request=self).annotation_detail(check.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))